package com.ruoyi.celoan.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.celoan.dto.ExTradeEffectCreditDetailDTO;
import com.ruoyi.celoan.entity.DalianDataReportKeyEntity;
import com.ruoyi.celoan.entity.ExTradeEffectCreditDetailEntity;
import com.ruoyi.celoan.handanUtils.HanDanHttpUtils;
import com.ruoyi.celoan.handanUtils.response.CountryPlatformResponse;
import com.ruoyi.celoan.mapper.EcDalianDataCreditDetailMapper;
import com.ruoyi.celoan.service.ExTradeEffectCreditDetailService;
import com.ruoyi.celoan.service.IEcDalianDataReportLogsService;
import com.ruoyi.celoan.util.ListUtils;
import com.ruoyi.manage.organization.domain.OcOrderCredit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExTradeEffectCreditDetailServiceImpl extends ServiceImpl<EcDalianDataCreditDetailMapper, ExTradeEffectCreditDetailEntity> implements ExTradeEffectCreditDetailService {
    private static final Logger log = LoggerFactory.getLogger(ExTradeEffectCreditDetailServiceImpl.class);
    @Autowired
    private IEcDalianDataReportLogsService dalianDataReportLogsService;
    private static String url = "/api/upload/creditLending";

    public void getHanDanDataReport(String interfaceName, DalianDataReportKeyEntity entity, String areaCode, String token) {
        String startDate = LocalDateTime.now().plusDays(-2L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        QueryWrapper<ExTradeEffectCreditDetailEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("area_code", areaCode);
        queryWrapper.eq("isflag", Integer.valueOf(0));
        log.info("开始时间：" + startDate);
        List<ExTradeEffectCreditDetailEntity> list = ((EcDalianDataCreditDetailMapper) this.baseMapper).selectList((Wrapper) queryWrapper);
        List<ExTradeEffectCreditDetailDTO> listDto = Convert.toList(ExTradeEffectCreditDetailDTO.class, list);
        if (CollUtil.isNotEmpty(listDto)) {
            List<List<ExTradeEffectCreditDetailDTO>> retList = ListUtils.splitList(listDto, 50);
            this.dalianDataReportLogsService.addLogs(interfaceName + "接口上报数据开始", listDto.size() + "条");
            String date = LocalDate.now().toString();
            List<Long> updateList = new ArrayList<>();
            for (int i = 0; i < retList.size(); i++) {
                try {
                    List<ExTradeEffectCreditDetailDTO> reportList = retList.get(i);
                    List<Long> addlist = (List<Long>) reportList.stream().map(ret -> ret.getId()).collect(Collectors.toList());
                    this.dalianDataReportLogsService.addLogs(interfaceName + "明细日期:" + date + "上报id为：", JSONObject.toJSONString(addlist));
                    log.info("请求参数:" + JSONObject.toJSONString(reportList));
                    CountryPlatformResponse response = HanDanHttpUtils.creditLending(url, JSONObject.toJSONString(reportList), token);
                    this.dalianDataReportLogsService.addLogs(interfaceName + "明细返回结果:", JSONObject.toJSONString(response));
                    if (response.isApiStatus()) {
                        updateList.addAll(addlist);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("数据上报异常:" + e.getMessage());
                    this.dalianDataReportLogsService.addLogs(interfaceName + "上报数据上报错误信息", JSONObject.toJSONString(e.getMessage()));
                }
            }
            if (CollUtil.isNotEmpty(updateList)) {
                UpdateWrapper<ExTradeEffectCreditDetailEntity> updateWrapper = new UpdateWrapper();
                updateWrapper.in("id", updateList);
                updateWrapper.set("isflag", Integer.valueOf(1));
                update((Wrapper) updateWrapper);
                this.dalianDataReportLogsService.addLogs(interfaceName + "上报数据上报成功条数:", updateList.size() + "条");
            }
        }
    }

    @Override
    public void addCreditDetail(OcOrderCredit source){
        ExTradeEffectCreditDetailEntity target = new ExTradeEffectCreditDetailEntity();

        target.setId(source.getId());
        target.setUniscId(null);
        target.setEnterpriseName(null);
        target.setBankName(null);
        target.setBankCode(null);
        target.setCreditStatus(source.getCreditState());
        target.setCreditAmount(String.valueOf(source.getCreditAmount()));
        target.setCreditTime(String.valueOf(source.getCreditTime()));
        target.setLoanStatus(null);
        target.setLoanAmount(null);
        target.setLoanRate(null);
        target.setLoadTime(null);
        target.setProvince(null);
        target.setCity(null);
        target.setCountry(null);
        target.setIsMicroEnterpriseloan(null);
        target.setIsCreditLoan(null);
        target.setIsMicroEnterprise(null);
        target.setExternalSystemId(null);
        target.setAreaCode(null);
        target.setPlatformId(1306L);
        target.setProductName(null);
        target.setMaturity(null);

        boolean save = this.save(target);
    }
}


/* Location:              D:\tuodayun\workspace\work\celoan-third-data-report-service-2.0.0-SNAPSHOT.jar!\BOOT-INF\classes\com\celoan\service\impl\ExTradeEffectCreditDetailServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */
